
package com.shwread.android.qysw.step;


/**
 * Calculates and displays the distance walked.
 *
 * @author Levente Bagi
 */
public class DistanceNotifier implements StepListener {

    public interface Listener {
        public void valueChanged(double value);

        public void passValue();
    }

    private Listener mListener;

    double mDistance = 0;

    PedometerSettings mSettings;
    Utils mUtils;

    boolean mIsMetric;
    float mStepLength;

    public DistanceNotifier(Listener listener, PedometerSettings settings, Utils utils) {
        mListener = listener;
        mUtils = utils;
        mSettings = settings;
        reloadSettings();
    }

    public void setDistance(double distance) {
        mDistance = distance;
        notifyListener();
    }

    public void reloadSettings() {
        mIsMetric = mSettings.isMetric();
        mStepLength = mSettings.getStepLength();
        notifyListener();
    }

    public void onStep() {

        if (mIsMetric) {
            mDistance += (float) (// kilometers
                    mStepLength // centimeters
                            / 100000.0*2.5); // centimeters/kilometer
        } else {
            mDistance += (float) (// miles
                    mStepLength // inches
                            / 63360.0*2.5); // inches/mile
        }

        notifyListener();
    }

    private void notifyListener() {
        mListener.valueChanged(mDistance);
    }

    public void passValue() {
        // Callback of StepListener - Not implemented
    }


}

